package com.flyfish.ddns.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WriteLog {

	private static File foldfile = null;
	private static File file = null;
	static {
		String path = PropertiesUtil.getProperty("logPath");
		// 如果属性不存在，不进行执行
		if (path != null && !path.equals("") && !path.trim().equals("")) {
			String foldPath = path.substring(0, path.lastIndexOf("/"));
			String filePath = path;
			foldfile = new File(foldPath);
			file = new File(filePath);
			// 创建文件夹
			if (!foldfile.exists()) {
				if (!foldfile.isDirectory()) {
					foldfile.mkdirs();
				}
			}
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 写入日志信息
	 * 
	 * @param str
	 * @throws IOException
	 */
	public static void writeLogInfo(String str) throws IOException {
		Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		writer.write(sdf.format(new Date()) + ":" + str + "\r\n");
		writer.flush();
		writer.close();
	}

	public static void main(String[] args) {
		try {
			WriteLog.writeLogInfo("aaa");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
